<template>
  <div class="box" ref="box">
    <el-row>
      <el-col :span="24">
        <el-card shadow="never">
          <el-form :inline="true" :model="formInline" class="demo-form-inline">
            <el-form-item>
              <el-input v-model="formInline.title" placeholder="标题"></el-input>
            </el-form-item>
            <el-form-item>
              <el-button type="primary" @click="onSubmitTitle" size="small" icon="el-icon-search">查询</el-button>
              <el-button type="primary" size="small" icon="el-icon-plus"
                         @click="dialogVisible = true;dialogTitle = '添加';resetForm('ruleForm')"
              >添加
              </el-button>
              <el-button type="danger" size="small" icon="el-icon-delete" @click="deleteItem()">删除</el-button>
            </el-form-item>
            <div class="right">
              <el-form-item>
                <el-checkbox v-model="checkedLine">斑马纹</el-checkbox>
              </el-form-item>
              <el-form-item>
                <el-checkbox v-model="checkedColum">边框（可拉伸列）</el-checkbox>
              </el-form-item>
              <el-form-item>
                <el-button icon="el-icon-full-screen" @click="handleFull"></el-button>
              </el-form-item>
              <el-form-item>
                <el-popover
                  placement="bottom-start"
                  width="200"
                  trigger="hover"
                >
                  <el-button icon="el-icon-set-up" slot="reference"></el-button>
                  <el-radio-group v-model="radio">
                    <el-radio :label="3">大</el-radio>
                    <el-radio :label="6">中</el-radio>
                    <el-radio :label="9">小</el-radio>
                  </el-radio-group>
                </el-popover>
              </el-form-item>
              <el-form-item>
                <el-popover
                  placement="bottom-start"
                  width="150"
                  trigger="hover"
                >
                  <el-button icon="el-icon-setting" slot="reference"></el-button>
                  <div class="check">
                    <el-checkbox-group v-model="checkList" @change="selectBox">
                      <el-checkbox label="0" disabled>标题</el-checkbox>
                      <el-checkbox label="1">作者</el-checkbox>
                      <el-checkbox label="2">评级</el-checkbox>
                      <el-checkbox label="3">点击量</el-checkbox>
                      <el-checkbox label="4">时间</el-checkbox>
                    </el-checkbox-group>
                  </div>
                </el-popover>
              </el-form-item>
            </div>
          </el-form>
          <el-col :span="24" class="dialog">
            <el-dialog :title="dialogTitle" :visible.sync="dialogVisible">
              <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
                <el-form-item label="标题" prop="title">
                  <el-input type="text" v-model="ruleForm.title"></el-input>
                </el-form-item>
                <el-form-item label="作者" prop="name">
                  <el-input type="text" v-model="ruleForm.name"></el-input>
                </el-form-item>
              </el-form>
              <span slot="footer" class="dialog-footer">
                <el-button @click="dialogVisible = false">取 消</el-button>
                <el-button type="primary" @click="submitForm('ruleForm')">确 定</el-button>
              </span>
            </el-dialog>
          </el-col>

          <el-col :span="24" class="table">
            <el-table
              ref="multipleTable"
              :data="tableData.slice((pages.pageNum-1)*pages.pageSize,pages.pageNum*pages.pageSize)"
              :stripe="checkedLine"
              :border="checkedColum"
              tooltip-effect="dark"
              style="width: 100%"
              :size="radio===9?'mini':''||radio===6?'small':''||radio===3?'medium':''"
              @selection-change="handleSelectionChange"
            >
              <el-table-column
                type="selection"
                width="55"
              >
              </el-table-column>
              <el-table-column label="序号" type="index" width="60"></el-table-column>

              <el-table-column label="标题" prop="title"></el-table-column>
              <el-table-column label="作者" prop="name" v-if="valueList.includes('1')"></el-table-column>
              <el-table-column label="评级" prop="rate" v-if="valueList.includes('2')">
                <template slot-scope="scope">
                  <el-rate
                    :max="3"
                    :value="scope.row.rate"
                    :colors="colors"
                  >
                  </el-rate>
                </template>
              </el-table-column>
              <el-table-column label="点击量" sortable prop="clickNum" v-if="valueList.includes('3')">
              </el-table-column>
              <el-table-column label="开关" prop="switch" dateFormat="datetime">
                <template slot-scope="scope">
                  <el-switch
                    @click.native="clickSwitch(scope.row)"
                    :value="scope.row.switch"
                  >
                  </el-switch>
                </template>
              </el-table-column>
              <el-table-column label="状态" prop="state" dateFormat="datetime">
                <template slot-scope="scope">
                  <el-tag v-if="scope.row.state==='draft'">{{ scope.row.state }}</el-tag>
                  <el-tag type="success" v-else-if="scope.row.state==='published'">{{ scope.row.state }}</el-tag>
                  <el-tag type="danger" v-else>{{ scope.row.state }}</el-tag>
                </template>
              </el-table-column>
              <el-table-column label="时间" prop="date" dateFormat="datetime" width="150" v-if="valueList.includes('4')">
              </el-table-column>
              <el-table-column label="操作" width="150">
                <template slot-scope="scope">
                  <div>
                    <el-button
                      size="mini"
                      @click="handleEdit(scope.row)"
                    >编辑
                    </el-button>
                    <el-button
                      size="mini"
                      @click="deleteItem(scope.row)"
                    >删除
                    </el-button>
                  </div>
                </template>
              </el-table-column>
            </el-table>
          </el-col>
          <Pagination :total="total" :pageNum.sync="pages.pageNum"
                      :limit.sync="pages.pageSize"
                      :pageSizes="pageSizes"
          ></Pagination>
        </el-card>
      </el-col>
    </el-row>
  </div>
</template>

<script>
import axios from 'axios'
import Pagination from '@/components/Pagination/Pagination'
import screenFull from 'screenfull'

export default {
  name: 'custom',
  components: { Pagination },
  data() {
    return {
      checkList: ['0', '1', '2', '3', '4'],
      valueList: ['0', '1', '2', '3', '4'],
      dialogTitle: '',
      isFull: false,
      radio: 3,
      colors: ['#99A9BF', '#F7BA2A', '#FF9900'],
      ruleForm: {
        name: '',
        title: ''
      },
      rules: {
        title: [
          { required: true, message: '请输入标题', trigger: 'blur' }
        ],
        name: [
          { required: true, message: '请输入作者', trigger: 'blur' }
        ]
      },
      pages: {
        pageNum: 1,
        pageSize: 10
      },
      total: 0,
      pageSizes: [10, 20, 30, 40, 50, 100],
      multipleSelection: [],
      tableData: [],
      dataList: [],
      loading: false,
      dialogVisible: false,
      checkedLine: false,
      checkedColum: false,
      formInline: {
        title: ''
      }
    }
  },
  methods: {
    resetForm(formName) {
      this.$nextTick(() => {
        this.$refs[formName].resetFields()
      })
    },
    handleFull() {
      const el = document.querySelector('.app-main')
      if (screenFull.isEnabled && !this.isFull) {
        screenFull.toggle(el) // 全屏切换
      }
    },
    selectBox(val) {
      this.valueList = val
    },
    fetchData() {
      //请求接口加载表格数据
      this.loading = true
      axios.get('http://route.showapi.com/60-27').then(res => {
        this.tableData = res.data._data.data.rows
        this.dataList = res.data._data.data.rows
        this.$store.commit('user/saveData', res.data._data.data.rows)
        this.total = res.data._data.data.total
        this.loading = false
      }).catch(err => {
        this.loading = false
      })
    },
    clickSwitch(row) {
      row.switch = !row.switch
    },
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.dialogVisible = false
          this.$message({
            message: '模拟保存成功',
            type: 'success'
          })
        } else {
          return false
        }
      })
    },
    onSubmitTitle() {
      let list = []
      this.tableData = this.dataList
      for (let i = 0; i < this.tableData.length; i++) {
        if (this.tableData[i].title.indexOf(this.formInline.title) >= 0) {
          list.push(this.tableData[i])
        }
      }
      this.tableData = list
      this.total = this.tableData.length
    },
    handleEdit(row) {
      this.dialogVisible = true
      this.resetForm('ruleForm')
      this.$nextTick(() => {
        this.dialogTitle = '编辑'
        this.ruleForm.title = row.title
        this.ruleForm.name = row.name
      })
    },
    handleSelectionChange(val) {
      this.multipleSelection = val
    },
    deleteItem() {
      if (this.multipleSelection.length <= 0) {
        this.$message.error('未选中任何行')
      } else {
        this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.$message({
            type: 'success',
            message: '模拟删除成功!'
          })
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
      }
    }
  },
  mounted() {
    this.fetchData()
  }
}
</script>

<style scoped lang="scss">
.check {
  ::v-deep.el-checkbox-group .el-checkbox {
    padding: 10px 0;
  }
}

.box {
  //min-height: 100vh;
  ::v-deep.el-button i {
    font-size: 14px;
  }

  ::v-deep.el-form-item__label {
    font-weight: 400;
  }

  .dialog {
    ::v-deep.el-dialog {
      width: 580px;
    }
  }

  .table {
    padding: 20px 0px;

    ::v-deep.el-input {
      width: 380px;
    }


    ::v-deep.cell {
      font-size: 14px;
      font-weight: 400;
      text-align: center;
    }
  }

  .right {
    float: right;

    .el-button {
      border: none;

      ::v-deep i {
        font-size: 16px;
      }
    }
  }
}
</style>
